import logging
import unittest

import requests
from parameterized import parameterized
from api.regLoginApi import RegLoginApi
from api.RechargeApi import RechargeApi

from common.common_assert import common_assert, common_assert1
from common.html_util import html_util
from common.read_json import read_json


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        """
        方法前置
        """
        # 创建session对象
        self.session = requests.Session()
        # 实例化登录模块封装的类
        self.reg_login_api = RegLoginApi(self.session)
        body = {
            "keywords": "18708309001",
            "password": "123456a"
        }

        # 实现登录接口
        self.reg_login_api.user_login(body)
        # 实例化充值模块封装的类
        self.rechargeApi = RechargeApi(self.session)

    def tearDown(self) -> None:
        """
        方法后置
        """
        # 关闭session对象
        self.session.close()

    # 获取充值验证码测试用例
    @parameterized.expand(read_json('recharge', 'img_verify_code'))
    def test01_get_recharge_verify_code(self, case_name, r, exp_status_code):
        print(case_name)
        # 获取充值图片验证码
        res = self.rechargeApi.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(exp_status_code, res.status_code)

    # 充值测试用例
    @parameterized.expand(read_json('recharge', 'recharge'))
    def test02_recharge(self, case_name, body, exp_status_code, exp_status, description):
        print(case_name)
        logging.info(case_name)
        # 登录
        # 获取充值图片验证码
        self.rechargeApi.get_recharge_verify_code(4)
        # 充值
        res = self.rechargeApi.recharge(body)
        # 断言

        common_assert1(self, res, exp_status_code, exp_status, description,case_name)

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        # 获取充值图片验证码
        self.rechargeApi.get_recharge_verify_code(4)
        # 充值
        body = {
            "paymentType": "chinapnrTrust",
            "amount": "100000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        res = self.rechargeApi.recharge(body)
        # 获取第三方开户接口的url
        a = html_util(response=res)
        # 获取第三方开户接口的请求体数据

        # 获取第三方接口返回的响应结果
        res = self.rechargeApi.third_recharge(url=a[0], data=a[1])

        # 断言
        self.assertEqual(200, res.status_code)
